/**
 * @desc
 *
 * @使用场景
 *
 * @author  杨晓东[of1081]
 * @company qianmi.com
 * @Date    16/11/28
 **/
'use strict';
 let svgRepoUtil = module.exports={};

let svgMap  = {
  
};

/**
 * 从源代码文件中提取SVG信息..
 * @param source
 * @param filePath
 */
svgRepoUtil.extraSvgInfo = ({source ,filePath})=>{

 //
 let totalSvgs =  [],begindex  = 0 , totalIndex =source.length-1;
 while (begindex < totalIndex){
  let {endIndex,svgs} =  extraOnce({source,begindex});
  totalSvgs =totalSvgs .concat(svgs);
  begindex= endIndex;
 }
 
 if(totalSvgs.length > 0 ){
  console.log('#### 提取出来的SVG值 为::',filePath,totalSvgs);
  svgMap[filePath] =totalSvgs;
 }
}


/**
 * 通过文件名获取 SVG信息
 * @param filePath
 * @return {*|Array}
 */
svgRepoUtil.getSvgsByFilePath =  (filePath)=>{
 return svgMap[filePath] || []
}


let _SvgFlag = 'x-site-new-qIcon';
/**
 * 从内容中提取出来..
 * @param source
 * @param begindex
 * @return {{endIndex: number, svgs: Array}}
 */
function extraOnce ({source, begindex = 0}){
 
 let svgs  = [] ,_end = 0;
 let _flagIndex = source.indexOf(_SvgFlag,begindex);
 if(_flagIndex >=0){
  
  _end  = source.indexOf('\'',_flagIndex);
  
  if(_end === -1){
   _end = source.length-1;
  }
  
  let _end2 =source.indexOf('\"',_flagIndex) ;
  if(_end2 === -1 ){
   _end2  = source.length-1;
  }
  
  if(_end >_end2 ){
   _end = _end2;
  }
  
  let classNames = source.substring(_flagIndex+_SvgFlag.length,_end);
  
  if(classNames){
   let classItems =  classNames.trim().split(/ +/);
   for(let i =0,ilen=classItems.length ; i<ilen; i++){
    if(classItems[i].startsWith('xSite-')){
     svgs.push(classItems[i]);
    }
   }
  }
 }else {
  _end= source.length-1;
 }
 
 
 return {endIndex:_end,svgs};
}